var prefix = jsctx + "/chargingStatistics";

var chargingStatistics = (function () {
    function init() {
        listEchars();
        load();
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listOrderChargingStatistics",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    cache: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            startTime: $("#startTime").val().trim(),
                            endTime: $("#endTime").val().trim(),
                            bizType:$("#bizType").val().trim(),
                            elecStationId: $("#elecStationId").val().trim(),
                            userType:$("#userType").val().trim(),
                            code:$("#code").val().trim()
                        };
                    },
                    columns: [
                        {
                            title: '时段', field: '', width: '50', align: 'left',
                            formatter: function (value, row, index) {
                                return row.hours + "~" + (row.hours + 1);
                            }
                        },
                        {field: 'beginTime', title: '开始时间', align: 'left', width: '100'},
                        {field: 'endTime', title: '结束时间', align: 'left', width: '100'},
                        {field: 'orderCount', title: '订单数', align: 'left', width: '50'},
                        {field: 'lastTimeCount', title: '总时长', align: 'left', width: '50'},
                        {field: 'totalMoneyCount', title: '总价格', align: 'left', width: '50'},
                        {field: 'payAmountCount', title: '实付', align: 'left', width: '50'},
                        {field: 'totalPower', title: '电量', align: 'left', width: '50'},
                        {field: 'totalElecMoney', title: '电费', align: 'left', width: '50'},
                        {field: 'totalSeviceMoney', title: '服务费', align: 'left', width: '50'},
                        {field: 'discountAmount', title: '优惠金额', align: 'left', width: '50'},
                    ]
                });
    }

    function reLoad() {
        listEchars();
        $('#exampleTable').bootstrapTable('refresh');
    }

    function goBack() {
        window.history.back();
    }

    //获取数据
    function listEchars() {
        var myChart = echarts.init(document.getElementById('main'));
        myChart.showLoading();
        $.ajax({
            "url": prefix + "/listChargingStatistics",
            "method": "GET",
            "data": {
                startTime: $("#startTime").val().trim(),
                endTime: $("#endTime").val().trim(),
                bizType:$("#bizType").val().trim(),
                elecStationId: $("#elecStationId").val().trim(),
                userType:$("#userType").val().trim(),
                code:$("#code").val().trim()
            },
            "success": function (data) {
                myChart.hideLoading();
                d(data, myChart);
            }
        })
    }


    function d(data, myChart) {
        // 基于准备好的dom，初始化echarts实例
        var hours = [];
        var time = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23];
        var chargeCapacity = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];


        for (data, i = 0; i < data.length; i++) {
            hours.push(data[i].hours)
        }

        for (hours, i = 0; i < hours.length; i++) {
            chargeCapacity.splice(hours[i], 1, data[i].chargeCapacity);
        }
        console.log(chargeCapacity)

        myChart.setOption({
            title: {
                text: '充电量统计',
            },
            xAxis: {
                type: 'category',
                data: time
            },
            yAxis: {
                type: 'value',
            },
            series: [{
                data: chargeCapacity,
                type: 'line'
            }]
        })
    }


    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad
    };
})();
